<?php

namespace Chap5Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;

class HelloController extends Controller
{
	/**
	 * 一个简单的Controller
	 * @Route("/hello/{name}", name="hello_index")
	 * @param  [type] $name [description]
	 * @return [type]       [description]
	 */
	public function indexAction($name)
	{
		return new Response("<h1>hello, ".$name."</h1>");
	}


	/**
	 * 页面跳转
	 * @Route("/hello/gotopage")
	 * @return [type] [description]
	 */
	public function gotopageAction()
	{
		return $this->redirectToRoute('hello_index');
		return $this->redirectToRoute("hello_index", array(), 301);
		return $this->redirectToRoute("hello_index", array(), 302);
		return $this->redirect('http://symfony.com');
	}

	/**
	 * 模板渲染
	 * @return [type] [description]
	 */
	public function templaterenderAction()
	{
		$assign = array();
		return $this->render("hello/templaterender.html.twig", $assign);

		return $this->render('Chap5Bundle:hello:templaterender.html.twig', $assign);
	}


	/**
	 * 访问服务
	 */
	public function accessserviceAction()
	{
		//查看所有存在的服务
		//php56 bin/console debug:container
		$templating = $this->get('templating');
		$router = $this->get('router');
		$mailer = $this->get('mailer');

		$form_parameter = $this->getParameter('app.mailer.from');
		
	}


	/**
	 * 抛出异常
	 * @throws \Exception
	 */
	public function errorpageAction()
	{
		throw  $this->createNotFoundException('The product does not exist');
		throw new \Exception('Somthing went wrong!');
	}


	/**
	 * 请求Request对象
	 * @param Request $request
	 */
	public function queryparamAction(Request $request)
	{
		var_dump($request);
		$page = $request->query->get('page', 1);
	}


	/**
	 * Session管理
	 * @param Request $request
	 */
	public function sessionmanageAction(Request $request)
	{
		$session = $request->getSession();

		//设置session
		$session->set('foo', 'bar');

		//获取session
		$foobar = $session->get('foobar', array());


	}


	/**
	 * Flash message
	 * @param Request $request
	 */
	public function flashmessageAction(Request $request)
	{
		$this->addFlash(
			'notice',
			'Your changes wree saved!'
		);

		/*
		 {% for flash_message in app.session.flashBag.get('notice') %}
		    <div>{{flash_message}}</div>
		 {% endfor%}
		 */
	}


	/**
	 * Request对象
	 * @param Request $request
	 */
	public function requestObjectAction(Request $request)
	{
		//是否是ajax请求
		$flag = $request->isXmlHttpRequest();

		$preferLang = $request->getPreferredLanguage(array('en', 'fr'));

		$getparam = $request->query->get('page');
		$postparam = $request->request->get('page');

		//$_SERVER变量
		$host = $request->server->get('HTTP_HOST');

		//上传文件
		$uploadFiles = $request->files->get('file');

		//COOKIE
		$setcookie = $request->cookies->set('expire', time()+100);
		$getcookie = $request->cookies->get('PHPSESSID');


		//HTTP Header
		$header_host = $request->headers->get('host');
		$header_contenttype = $request->headers->get('content_type');
	}


	/**
	 * Response对象
	 */
	public function responseObjectAction()
	{
		//simple response
		$response = new Response('hello', Response::HTTP_OK);

		//css response
		$response2 = new Response('<style></style>');
		$response2->headers->set('Content-Type', 'text/css');

		//json response
		$response3 = new JsonResponse(array('name'=>'xudong'));
		//new BinaryFileResponse();
		//new StreamedResponse();
	}


	/**
	 * 调用其他控制器中的方法
	 */
	public function forwardingAction()
	{
		$response = $this->forward('Chap5Bundle:Default:index', array());

		var_dump($response);
	}


	/**
	 * CSRF验证机制
	 */
	public function validateCSRFAction(Request $request)
	{
		$token = $request->get('token_id');
		if ($this->isCsrfTokenValid('token_id', $token)) {

		}
	}



}
